﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_Framework.Methods;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Ekton_Manage_Business
{
    public class APISysMsg : IApiSelectSysMsg
    {
        private readonly SerializeParameter _serialize;
        private readonly IBaseRepository<msg_user, int> _baseMaterialRepository;
        private readonly IBaseRepository<user_msg, int> _baseusermsgRepository;

        public APISysMsg(IBaseRepository<msg_user, int> baseMaterialRepository, SerializeParameter serialize, IBaseRepository<user_msg, int> baseusermsgRepository)
        {
            _baseMaterialRepository = baseMaterialRepository;
            _baseusermsgRepository = baseusermsgRepository;
            _serialize = serialize;
        }

        /// <summary>
        /// 查询前端用户的所有消息
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<Tuple<List<user_msg>, int>> GetMsgList(QueryDataParame query)
        {
            var dicData = await _serialize.ParameConvertToDict(query.parame);
            Expression<Func<user_msg, bool>> expression = m => true;
            if (dicData.ContainsKey("user_id") && !string.IsNullOrEmpty(dicData["user_id"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.user_id == dicData["user_id"].ObjToInt());
            }
            var result = await _baseusermsgRepository.QueryPageData(query, expression);
            return result;
        }

        /// <summary>
        /// 查看详细消息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Tuple<List<user_msg>, int>> GetUserMsg(QueryDataParame query)
        {
            var dicData = await _serialize.ParameConvertToDict(query.parame);
            Expression<Func<user_msg, bool>> expression = m => true;
            if (dicData.ContainsKey("user_id") && !string.IsNullOrEmpty(dicData["user_id"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.user_id == dicData["user_id"].ObjToInt());
            }
            if (dicData.ContainsKey("state") && !string.IsNullOrEmpty(dicData["state"].ToString()))
            {
                expression = expression.ExpressionAnd(m => m.state == dicData["state"].ObjToInt());
            }
            var result = await _baseusermsgRepository.QueryPageData(query, expression, m => m.look_state);
            return result;
        }

        /// <summary>
        /// 用户点击过之后，将这条消息的状态改为已读
        /// </summary>
        /// <returns></returns>
        public async Task<Message> PutSysMsgState(int id)
        {
            var res = await _baseMaterialRepository.QuerySingleData(m => m.id == id);
            if (res == null) return new Message(false, 202);
            res.look_state = 1;//将这条消息的状态改为已读
            var result = await _baseMaterialRepository.UpdateSingleData(res);
            return new Message(result);
        }

        /// <summary>
        /// 获取用户未读消息的个数
        /// </summary>
        /// <param name="user_id"></param>
        /// <returns></returns>
        public async Task<Message> GetMsgCount(int user_id)
        {
            var count = await _baseMaterialRepository.QueryCount(m => m.user_id == user_id && m.look_state == 0);
            return new Message(count);
        }

        /// <summary>
        /// 批量修改系统消息状态
        /// </summary>
        /// <param name="id"></param>
        /// <remarks>user_id</remarks>
        /// <returns></returns>
        public async Task<Message> PutAllSysMsgState(int id)
        {
            var res = await _baseMaterialRepository.QueryTableList(m => m.user_id == id);
            if (res == null) return new Message(false, 202);
            foreach (var item in res)
            {
                item.look_state = 1;
            }
            var result = await _baseMaterialRepository.UpdateManyData(res);
            return new Message(result);
        }
    }
}
